package com.example.demo.study.lambda;


import java.util.Objects;

public interface Function<T, R> {

    R apply(T t);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    //注意: t->t是(t)->t的简写
    //t->t是作为方法identity的返回值的,也就是Function类型对象
    //类似于这样的写法:Function<Object, Object> f = t->t;
    //那么f.apply("test") 返回字符串"test"
    //传入什么则返回什么
    static <T> Function<T, T> identity() {
        return t -> t;
    }

}
